package cn.com.adcc.utils;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.zeroturnaround.zip.ZipUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by anzilu on 2015/10/13.
 */
public class ZipHandler implements Runnable {

    private static Logger logger = Logger.getLogger(ZipHandler.class);
    private String source = "";
    private String destination = "";

    public ZipHandler(String source, String destination) {

        this.source = source;
        this.destination = destination;
    }

    @Override
    public void run() {

        compressFiles();

//        ZipUtil.pack(new File(source),new File(destination));
        DateTime endcomp = new DateTime(DateTimeZone.UTC);
        System.out.println(endcomp.getMillis());
//        FileOutputStream fos = null;
//        try {
//            fos = new FileOutputStream(source + ".zip");
//            ZipOutputStream zos = new ZipOutputStream(fos);
//            zipDir(source,zos);
//            zos.flush();
//            fos.flush();
//            zos.close();
//            fos.close();
//            DateTime endcomp = new DateTime(DateTimeZone.UTC);
//            System.out.println(endcomp.getMillis());
//        }
//        catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public boolean compressFiles() {

        try {
            String zipMiddle=source+".tmp";
            ZipFile zipFile = new ZipFile(zipMiddle);
            File root = new File(source);

            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
            zipFile.addFolder(root, parameters);

            DateTime middlecomp = new DateTime(DateTimeZone.UTC);
            System.out.println(middlecomp.getMillis());

            ZipFile zipFile2 = new ZipFile(destination);
            ZipParameters parameters1=new ZipParameters();
            parameters1.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters1.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
            zipFile2.addFile(new File(zipMiddle),parameters1);

            return true;

        } catch (ZipException e) {
            logger.error(e.getMessage());
            return false;
        }
    }

    public void zipDir(String dir2zip, ZipOutputStream zos) {
        try {
            //create a new File object based on the directory we have to zip
            File zipDir = new File(dir2zip);
            //get a listing of the directory content
            String[] dirList = zipDir.list();
            byte[] readBuffer = new byte[2156];
            int bytesIn = 0;
            //loop through dirList, and zip the files
            for (int i = 0; i < dirList.length; i++) {
                File f = new File(zipDir, dirList[i]);
                if (f.isDirectory()) {
                    //if the File object is a directory, call this
                    //function again to add its content recursively
                    String filePath = f.getPath();
                    zipDir(filePath, zos);
                    //loop again
                    continue;
                }
                //if we reached here, the File object f was not a directory
                //create a FileInputStream on top of f
                FileInputStream fis = new FileInputStream(f);
                ZipEntry anEntry = new ZipEntry(f.getPath());
                //place the zip entry in the ZipOutputStream object
                zos.putNextEntry(anEntry);
                //now write the content of the file to the ZipOutputStream
                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                }
                //close the Stream
                fis.close();
            }
        } catch (Exception e) {
            //handle exception
        }
    }

    public void snappyZip(){


    }
}